package command;

import java.net.InetAddress;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.*;
import command.Command;
import command.CommandException;
import db.CommonConnect;

import common.CommonUtil;
import common.Crypto;
import common.EDUBOX_Baese64_Crypto;
import common.UserSession;
import db.QueryManager;
import db.DAO;

/**
 * Command 인터페이스를 implement 하여 구현된 command.
 */
public class PreptestModifyCommand extends Command {

	private String next;
	
	public PreptestModifyCommand(String next) {
		this.next=next;
	}

	/**
	 * 실제 요청을 받아 처리하는 메소드
	 * 요청을 받으면  요청을 처리하고 next 페이지를 돌려줌
	 * 
	 * @param req HttpServletRequest
	 * @throws CommandException
	 * @return next 이동할 페이지
	 */
	public String execute(HttpServletRequest req)
	throws CommandException {
		ArrayList list =  null;
		HashMap listMap = new HashMap();	
	
		Connection con = null;  
		try {		
					
						
			String rating_grade = CommonUtil.nvl(req.getParameter("rating_grade"));	
			String rating_cont = CommonUtil.nvl(req.getParameter("rating_cont"));	
			String user_id = CommonUtil.nvl(req.getParameter("user_id"));	
			String member_id = CommonUtil.nvl(req.getParameter("member_id"));	
			String member_test_code = CommonUtil.nvl(req.getParameter("member_test_code"));
		
			String rater_comment_sel[] = req.getParameterValues("rater_comment_sel");
			int rater_comment_cnt = Integer.parseInt(CommonUtil.nvl(req.getParameter("rater_comment_cnt"),"0"));
			
			String rater_comment[] = {"",""}; 
						
			con = (new CommonConnect()).getConnection_ds2();
			con.setAutoCommit(false);
			
			String[] param=null;
			String queryStr = QueryManager.getQuery("opictest_modify.query");
			param= new String[5];
			param[0] = member_test_code;	
			param[1] = member_id;	
			param[2] = user_id;
			param[3] = rating_grade;
			param[4] = rating_cont;			
			
			list = DAO.select(con,queryStr,param);
			
			listMap.put("mainContent",list);
			logger.debug("queryStr ==> "+"opictest_modify.query");		
			
			
			for(int i =0 ; i < rater_comment_cnt; i++){
				
				System.out.println("넘겨온값 =====>" + " rater_comment_sel===>"+rater_comment_sel[i]);
				
				rater_comment[0] = ""; 
				rater_comment[1] = "";
				rater_comment = rater_comment_sel[i].split("-");
				
				param=null;
				param= new String[4];
				param[0]=member_test_code;
				param[1]=rater_comment[0];
				param[2]=rater_comment[1];
				param[3] = user_id;
					
				queryStr = "call sp_sp1201_comment_insert(?,?,?,?)";
				
				DAO.update(con,queryStr,param);
				
				logger.debug("queryStr ==> "+"call sp_sp1201_comment_insert(?,?,?,?)");		
			}
		     
// 리퀘스트에 모두 담는다.
			req.setAttribute("list_map",listMap);

		}catch (Exception ex) {
			try{
				con.rollback();
			}catch(Exception ee){}
			ex.printStackTrace();
			throw new CommandException(ex.getMessage());
		}finally {
			try{
				if (con!=null) con.setAutoCommit(true);
				if (con!=null) con.close();
			}catch(Exception ee){}
		}
		return next;
	}
	


}
